#include "../Include/main.h"
#include "../Include/class.h"
int cn=0;
int cm;
void seekcourse()
{
	int i,item,flag;
	char s1[20];
	printf("-----1.按课程编号查询-----\n");
	printf("-----2.按课程名称查询-----\n");
	printf("-------3.退出本菜单-------\n");
	while(1)
	{
		printf("请选择子菜单编号："); 
		scanf("%d",&item);
		flag=0;
		switch(item)
		{
			case 1:
			printf("请输入要查询的课程编号：\n");
			scanf("%s",&s1);
			getchar();
			for(i=0;i<cn;i++){
				if(strcmp(course[i].cnum,s1)==0)
				{
					flag=1;
					printf("课程编号\t课程名称\t课程性质\t课程学时\t课程学分\t课程已选人数\t课程人数上限\n"); 
					printf("%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n\n",course[i].cnum,course[i].cname,course[i].cnature,course[i].ctime,course[i].cscore,course[i].nelen,course[i].nummax);
				}
				if(flag==0)
				printf("该课程编号不存在！\n");
				break;
				case 2:
				printf("请输入要查询的课程的名称：\n");
				scanf("%s",&s1);
				for(i=0;i<cn;i++)
				if(strcmp(course[i].cname,s1)==0)
				{
					flag=1;
					printf("课程编号\t课程名称\t课程性质\t课程学时\t课程学分\t课程已选人数\t课程人数上限\n"); 
					printf("%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n",course[i].cnum,course[i].cname,course[i].cnature,course[i].ctime,course[i].cscore,course[i].nelen,course[i].nummax);
				 }
				 if(flag==0)
				 printf("该课程名称不存在！\n");
				 break;
				 case 3:return;
				 default:printf("请在1-3之间选择\n"); 
			}
		 } 
	 } 
}
void insertcourse()
{
	int j;
	printf("请输入待增加的课程数：\n");
	scanf("%d",&cm);
	for(j=cn;j<cn+cm;j++)
	{
		printf("请输入第%d个课程编号：\n",j+1);
		scanf("%s",&course[j].cnum);
		printf("请输入第%d个课程名称：\n",j+1);
		scanf("%s",&course[j].cname);
		printf("请输入第%d个课程性质：\n",j+1);
		scanf("%s",&course[j].cnature);
		printf("请输入第%d个课程学时：\n",j+1);
		scanf("%s",&course[j].ctime);
		printf("请输入第%d个课程学分：\n",j+1);
		scanf("%s",&course[j].cscore);
		printf("请输入第%d个课程已选人数：\n",j+1);
		scanf("%s",&course[j].nelen);
		printf("请输入第%d个课程人数上限：\n",j+1);
		scanf("%s",&course[j].nummax);
	}
	cn+=cm;
	printf("信息增加完毕！\n\n");
	sortcourse();
}
void modifycourse()
{
	int i,item,num;
	int a;
	char s1[LEN+1],s2[LEN+1],s3[LEN+1],s4[LEN+1];
	printf("请输入要修改的课程编号：\n");
	scanf("%s",s1);
	for(i=0;i<cn;i++)
	if(strcmp(course[i].cnum,s1)==0)
	{
		num=i;
		printf("------------------\n");
		printf("1.修改学时\n");
		printf("2.修改课程学分\n");
		printf("3.修改课程人数上限\n");
		printf("4.退出本菜单\n");
		printf("------------------\n");
	while(1)
	{
		printf("请选择子菜单编号：");
		scanf("%d",&item);
		switch(item)
		{
			case 1:
			printf("请输入新的学时：\n");
			scanf("%s",s2);
			strcpy(course[num].ctime,s2);break;
			
			case 2:
			printf("请输入新的课程学分：\n");
			scanf("%s",s3);
			strcpy(course[num].cscore,s3);break;
			
			case 3:
			printf("请输入新的课程人数上限：\n");
			scanf("%s",s4);
			strcpy(course[num].nummax,s4);break;
			
			case 4: return ;
			default:printf("请在1-4之间选择\n");
		}
    }
  }
	
}

void delcourse()
{
	int i,j,flag=0;
	char s1[LEN+1];
	printf("请输入要删除课程的名称：\n");
	scanf("%s",s1);
	for(i=0;i<cn;i++)
	if(strcmp(course[i].cname,s1)==0)
	{
		flag=1;
		for(j=1;j<cn-1;j++)
		{
			strcpy(course[j].cnum,course[j+1].cnum);
			strcpy(course[j].cname,course[j+1].cname);
			strcpy(course[j].cnature,course[j+1].cnature);
			strcpy(course[j].ctime,course[j+1].ctime);
			strcpy(course[j].cscore,course[j+1].cscore);
			strcpy(course[j].nelen,course[j+1].nelen);
			strcpy(course[j].nummax,course[j+1].nummax);
		}
	}
	if(flag==0)
		printf("该课程不存在！\n");
	if(flag==1)
	{
		printf("删除成功，显示结果请选择菜单\n");
		cn--;
	}
	printf("推出请按0\n");
	int a;
	scanf("%d",&a);
	switch(a)
	{
		case 0:
		return;
		default:
		printf("输入错误！");
	}
}

void displaycourse()
{
	int i;
	printf("共有%d个课程信息：\n",cn);
	if(cn!=0)
	{
		printf("课程编号\t课程名称\t课程性质\t课程学时\t课程学分\t课程已选人数\t课程人数上限\n");
		sortcourse();
		for(i=0;i<cn;i++)
		{
			printf("%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n",course[i].cnum,course[i].cname,course[i].cnature,course[i].ctime,course[i].cscore,course[i].nelen,course[i].nummax);
		}
	}
	else
	{
		printf("暂无课程信息\n"); 
	}
	printf("退出请按0\n");
	int a;
	scanf("%d",&a);
	if(a==0)
	return;
}

void sortcourse()
{
	int i,j,k,*p,*q,s;
	char temp[LEN+1],ctemp[20];
	for(i=0;i<cn;i++)
	{
		for(j=cn-1;j>i;j--)
		if(strcmp(course[j-1].cnum,course[j].cnum)>0)
		{
			strcpy(temp,course[j-1].cnum);
			strcmp(course[j-1].cnum,course[j].cnum);
			strcmp(course[j].cnum,temp);
			
			strcpy(temp,course[j-1].cname);
			strcmp(course[j-1].cname,course[j].cname);
			strcmp(course[j].cname,temp);
			
			strcpy(temp,course[j-1].cnature);
			strcmp(course[j-1].cnature,course[j].cnature);
			strcmp(course[j].cnature,temp);
			
			strcpy(temp,course[j-1].ctime);
			strcmp(course[j-1].ctime,course[j].ctime);
			strcmp(course[j].ctime,temp);
			
			strcpy(temp,course[j-1].cscore);
			strcmp(course[j-1].cscore,course[j].cscore);
			strcmp(course[j].cscore,temp);
			
			strcpy(temp,course[j-1].nelen);
			strcmp(course[j-1].nelen,course[j].nelen);
			strcmp(course[j].nelen,temp);
			
			strcpy(temp,course[j-1].nummax);
			strcmp(course[j-1].nummax,course[j].nummax);
			strcmp(course[j].nummax,temp);
		}
	}
}

void coursetable()
{
	FILE *fp = fopen("/home/wxy/MyProject/my-project/ModuleA/class.txt","r");
	if(fp==NULL){
		fprintf(stderr,"文件打开失败。\n");
		exit(EXIT_FAILURE);
	}
	char row[1000];
	char *token;
	int i=0;
	if(fgets(row, 1000, fp)!=NULL){
		while(fgets(row,1000,fp)!=NULL){
			sscanf(row,"%s%s%s%s%s%s%s",course[i].cnum,course[i].cname,course[i].cnature,course[i].ctime,course[i].cscore,course[i].nelen,course[i].nummax);
			printf(fp,"%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n",course[i].cnum,course[i].cname,course[i].cnature,course[i].ctime,course[i].cscore,course[i].nelen,course[i].nummax);
			i++;
			cn++;
		}
	}
}

void savecourse()//保存信息
{
	
	printf("文档保存请按1\n");
	printf("直接保存请按2\n");
	int a;
	scanf("%d",&a);
	if(a==1){ 
	FILE *fp; 
	fp= fopen("/home/wxy/MyProject/my-project/ModuleA/class.txt","w");//打开文档，写入
	fprintf(fp,"课程编号\t课程名称\t课程性质\t课程学时\t课程学分\t课程已选人数\t课程人数上限\n");
	for(int i=0;i<cn;i++)
	{
		fprintf(fp,"%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n",course[i].cnum,course[i].cname,course[i].cnature,course[i].ctime,course[i].cscore,course[i].nelen,course[i].nummax);
	}
	fclose(fp);
}else if(a==2)
{
	FILE *fp; 
	fp= fopen("/home/wxy/MyProject/my-project/ModuleA/class.txt","a");//打开文档，写入
	for(int i=0;i<cn;i++)
	{
		fprintf(fp,"%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n",course[i].cnum,course[i].cname,course[i].cnature,course[i].ctime,course[i].cscore,course[i].nelen,course[i].nummax);
	}
	fclose(fp);
}
}
